package lab3;

import java.util.ArrayList;

public class Exercise {
    private ArrayList<BinaryOperation> operations;
    private int count;
    private static final int OPERATION_NUMBER = 50;
    private static final int COLUMN_NUMBER = 5;
    
    public Exercise() {
        operations = new ArrayList<>();
        count = 0;
    }
    
    // 检查是否包含相同的算式
    public boolean contains(BinaryOperation operation) {
        for (BinaryOperation op : operations) {
            if (op.equals(operation)) {
                return true;
            }
        }
        return false;
    }
    
    // 生成加法习题
    public void generateAdditionExercise(int operationCount) {
        operations.clear();
        BinaryOperation opCreator = new BinaryOperation();
        
        for (int i = 0; i < operationCount; i++) {
            BinaryOperation anOperation = opCreator.generateAdditionOperation();
            while (contains(anOperation)) {
                anOperation = opCreator.generateAdditionOperation();
            }
            operations.add(anOperation);
        }
        count = operationCount;
    }
    
    // 生成减法习题
    public void generateSubstractExercise(int operationCount) {
        operations.clear();
        BinaryOperation opCreator = new BinaryOperation();
        
        for (int i = 0; i < operationCount; i++) {
            BinaryOperation anOperation = opCreator.generateSubstractOperation();
            while (contains(anOperation)) {
                anOperation = opCreator.generateSubstractOperation();
            }
            operations.add(anOperation);
        }
        count = operationCount;
    }
    
    // 生成混合习题
    public void generateExercise(int operationCount) {
        operations.clear();
        BinaryOperation opCreator = new BinaryOperation();
        
        for (int i = 0; i < operationCount; i++) {
            BinaryOperation anOperation = opCreator.generateBinaryOperation();
            while (contains(anOperation)) {
                anOperation = opCreator.generateBinaryOperation();
            }
            operations.add(anOperation);
        }
        count = operationCount;
    }
    
    // 格式化显示
    public void formatAndDisplay() {
        for (int i = 0; i < count; i++) {
            System.out.print((i + 1) + ". " + operations.get(i) + "=\t");
            if ((i + 1) % COLUMN_NUMBER == 0) {
                System.out.println();
            }
        }
        if (count % COLUMN_NUMBER != 0) {
            System.out.println();
        }
    }
    
    // 获取指定位置的算式
    public BinaryOperation getOperation(int index) {
        if (index >= 0 && index < count) {
            return operations.get(index);
        }
        return null;
    }
    
    // 获取习题数量
    public int size() {
        return count;
    }
}